<?php
namespace Cyfy\Modules\Videos\Forms;
use \Cyfy\Modules\Forms;

class Add extends Forms\Form
{
    const EG_LINK = "http://www.youtube.com/watch?v=oWr6HQC8bgs";
    protected $vid = array();
    
    function structure()
    {
        $this -> permission("Videos", "add and delete");
        $this -> stay = false;


        $i = new Forms\TextBox("title", self :: t(10));
        $i -> required = true;
        $i -> maxlenght = 255;
        $this -> AddItem($i);
       
        $i = new Forms\TextBox("value", self :: t(30));
        $i -> required = true;
        $i -> description = self :: t(41, array(self :: EG_LINK));
        $i -> maxlenght = 255;
        $this -> AddItem($i);
       
        $i = new Forms\TextArea("description", self :: t(20));
        $this -> AddItem($i);
        
       

        $b = new Forms\Button(self :: t(50));
        $this -> AddButton($b);
    }
    
    function validate()
    {
        $vid = \Cyfy\Modules\Videos\Module :: parseUrl($this -> get("value"));
        if(!$vid)
        {
            \Cyfy\Message :: set(self :: t(61, array($this->Get("value"))), \Cyfy\Message::ERROR);
            return false;
        }
        
        $this -> vid = $vid;
        
        return true;
    }
    
    function submit()
    {
        $values = array(
            'uid' => \Cyfy\Modules\Users\Current :: $uid,
            'time' => new \datetime(),            
            'type' =>  $this -> vid["type"],
            'value' =>  $this -> vid["value"],
            'title' => $this -> get('title'),
            'description' => $this -> get('description'),
        );
        \dibi :: query("INSERT INTO [:cyfy:Videos] %v", $values);
        \Cyfy\Message :: set(self :: t(70));
        $this -> onSubmit = "Cyfy.ajax('Videos', 'videos', '#videos', '');";
        return true;
    }
}
